<template>
	<view>
		<image src="../../../客服微信.jpg"></image>
	</view>
</template>

<script>
	import { mapState, mapActions, mapGetters } from 'vuex';
	import {createAttestEn,attestInfo,updateAttestEn} from '@/common/http.api.js'
	import {isEmpty} from '@/utils/utils';
	import config from '@/common/config';
	import jwt from '@/utils/jwt.js'
	export default {
		data() {
			return {
				disabled: false,
				fileListFront:[],
				fileListReverse:[],
				fileListHold:[],
				fileListVideo:[],
				form: {
					realName: '',
					ssn: '',
					realId:'',
					realPersonVideo:'',
					status:'',
					statusName:''
				},
				rules:{
					'realName': {
						type: 'string',
						required: true,
						message: this.pleaserealnameText,
						trigger: ['blur', 'change']
					},
					'ssn': {
						type: 'string',
						required: true,
						message: this.ssnnotemptyText,
						trigger: ['blur', 'change']
					},
					'realId': {
						type: 'string',
						required: true,
						message: this.realidnotemptyText,
						trigger: ['blur', 'change']
					},
				},
				locale:'',
				precautionsTitle:'',
				precautions1Text:'',
				precautions2Text:'',
				precautions3Text:'',
				precautions4Text:'',
				precautions5Text:'',
				actualnameText:'',
				identificationnumberText:'',
				contactqqText:'',
				idcardfrontText:'',
				idcardbackText:'',
				holdingIdcardfrontText:'',
				realvideoText:'',
				authrulesText:'',
				authrules1Text:'',
				authrules2Text:'',
				authrules3Text:'',
				approvalstatusText:'',
				notcertifiedText:'',
				submitText:'',
				pleaserealnameText:'',
				pleaseyouridnumberText:'',
				pleasecontactqqText:'',
				ssnText:'',
				ssnnotemptyText:'',
				realidText:'',
				fillrealidText:'',
				fillssnText:'',
				realidnotemptyText:'',
				reviewtimeText:'',
				that:this,
				authStatusDict:{
					0:this.$t('attest.pendingreview.text'),
					1:this.$t('attest.examinationpassed.text'),
					2:this.$t('attest.auditfailure.text')
				},
			};
		},
		async onLoad(){
			await this.getInfo();
		},
		async onShow(){
			this.refresh();
			this.locale = this.$i18n.locale;
			this.isLogin ? await this.getMemberInfo() : this.resetMemberInfo();
		},
		filters: {
			statusName(val,that) {
				if(that.status){
					return that.authStatusDict[val];
				}else{
					return that.notcertifiedText;
				}
			}
		},
		methods:{
			...mapActions(['getMemberInfo','resetMemberInfo','LogOut']),
			refresh(){
				this.isLogin = jwt.getLogin();
				this.precautionsTitle = this.$t('auth.precautions.text');
				this.precautions1Text = this.$t('auth.precautions1.text');
				this.precautions2Text = this.$t('auth.precautions2.text');
				this.precautions3Text = this.$t('auth.precautions3.text');
				this.precautions4Text = this.$t('auth.precautions4.text');
				this.precautions5Text = this.$t('auth.precautions5.text');
				this.actualnameText = this.$t('auth.actualname.text');
				this.identificationnumberText = this.$t('auth.identificationnumber.text');
				this.contactqqText = this.$t('auth.contactqq.text');
				this.idcardfrontText = this.$t('auth.idcardfront.text');
				this.holdingIdcardfrontText = this.$t('auth.holdingIdcardfront.text');
				this.idcardbackText = this.$t('auth.idcardback.text');
				this.realvideoText = this.$t('auth.realvideo.text');
				this.authrulesText = this.$t('auth.authrules.text');
				this.authrules1Text = this.$t('auth.authrules1.text');
				this.authrules2Text = this.$t('auth.authrules2.text');
				this.authrules3Text = this.$t('auth.authrules3.text');
				this.approvalstatusText = this.$t('auth.approvalstatus.text');
				this.notcertifiedText = this.$t('auth.notcertified.text');
				this.submitText = this.$t('common.submit.text');
				
				this.pleaserealnameText = this.$t('auth.pleaserealname.text');
				this.pleaseyouridnumberText = this.$t('auth.pleaseyouridnumber.text');
				this.pleasecontactqqText = this.$t('auth.pleasecontactqq.text');
				
				this.ssnText= this.$t('auth.ssn.text');
				this.ssnnotemptyText= this.$t('auth.ssnnotempty.text');
				this.realidText= this.$t('auth.realid.text');
				this.fillrealidText= this.$t('auth.fillrealid.text');
				this.fillssnText= this.$t('auth.fillssn.text');
				this.realidnotemptyText= this.$t('auth.realidnotempty.text');
				
				this.reviewtimeText= this.$t('auth.reviewtime.text');
			},
			// 删除图片
			deletePic(event) {
				this.fileListVideo.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.fileListVideo.length
				lists.map((item) => {
					this.fileListVideo.push({
						...item,
						status: 'uploading',
						message: this.$t('common.uploadloading.text')
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let json = JSON.parse(result);
					if(json.code === 0){
						let item = this.fileListVideo[fileListLen]
						this.fileListVideo.splice(fileListLen, 1, Object.assign(item, {
							status: 'success',
							message: '',
							url: json.data
						}))
						fileListLen++
					}
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: config.baseUrl + '/app/common/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						header: {
							"tenant-id": config.tenantId //token校验
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data)
							}, 1000)
						},
						complete: (res)=>{
							let json = JSON.parse(res.data);
							if(json.code != 0){
								this.$refs.uToast.show({
									message: json.msg,
									type: 'error',
								})
							}
						}
					});
				})
			},
			getInfo(){
				attestInfo().then(res => {
					if (res.code === 0 && res.data != null) {
						this.form = res.data;
						if(this.form.status === 1 || this.form.status === 0){
							this.disabled = true;
						}
						this.fileListVideo.push( {
							status: 'success',
							message: '',
							url: this.form.realPersonVideo
						});
					}
				});
			},
			submitForm(){
				let that = this;
				if(this.form.status && this.form.status === 1){
					this.$refs.uToast.show({
						message: that.$t('auth.doauth.text'),
						type: 'error',
					})
					return;
				}
				if(isEmpty(this.form.realName)){
					this.$refs.uToast.show({
						message: that.$t('auth.realnamenotempty.text'),
						type: 'error',
					})
					return;
				}
				if(isEmpty(this.form.ssn)){
					this.$refs.uToast.show({
						message: that.$t('auth.ssnnotempty.text'),
						type: 'error',
					})
					return;
				}
				if(isEmpty(this.form.realId)){
					this.$refs.uToast.show({
						message: that.$t('auth.realidnotempty.text'),
						type: 'error',
					})
					return;
				}
				if(this.fileListVideo.length === 0){
					this.$refs.uToast.show({
						message: that.$t('auth.videonotempty.text'),
						type: 'error',
					})
					return;
				}
				this.form.realPersonVideo = this.fileListVideo[0].url;
				if(this.form.id){
					updateAttestEn(this.form).then(res => {
						if (res.code === 0) {
							this.sending = true;
							this.$refs.uToast.show({
								message: that.$t('auth.submitsuccess.text'),
								type: 'success'
							})
							uni.hideLoading();
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/tabbar/home/home'
								});
							}, 1000);
						} else {
							uni.hideLoading();
							uni.showModal({
								showCancel: false,
								title: that.$t('auth.submiterr.text'),
								content: res.msg,
							});
						}
					});
				}else{
					createAttestEn(this.form).then(res => {
						if (res.code === 0) {
							this.sending = true;
							this.$refs.uToast.show({
								message: that.$t('auth.submitsuccess.text'),
								type: 'success'
							})
							uni.hideLoading();
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/tabbar/home/home'
								});
							}, 1000);
						} else {
							uni.hideLoading();
							uni.showModal({
								showCancel: false,
								title: that.$t('auth.submiterr.text'),
								content: res.msg,
							});
						}
					});
				}
			}
		}
	}
</script>

<style lang="scss">
.goshop-auth-container{
	padding: 20rpx;
	.goshop-auth-head{
		.goshop-auth-head-img{
			height: 440rpx;
		}
	}
	.goshop-auth-body{
		background-color: $goshop-color-auxiliary-one;
		padding: 20rpx;
		margin-top: 10rpx;
		color: $goshop-text-color-two;
	}
	.goshop-auth-form{
		background-color: $goshop-color-auxiliary-one;
		padding: 0 20rpx;
		margin-top: 10rpx;
	}
	.goshop-auth-rules{
		display: flex;
		flex-direction: column;
		justify-content: center;
		padding: 20rpx;
		margin-top: 10rpx;
		background-color: $goshop-color-auxiliary-one;
	}
	.goshop-auth-bottom{
		display: flex;
		align-items: center;
		justify-content: center;
		flex-direction: column;
		padding: 20rpx;
		color: $goshop-color-primary;
	}
	.goshop-auth-footer{
		
	}
}
</style>